package com.shgsh.server.client;
import com.shgsh.server.message.Message;
import com.shgsh.server.model.IMUser;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;



public class YQClient {
    private String id;
    public Socket s;
    public YQClient(String id){
        this.id=id;
    }
    public boolean sendLoginInfo(Object obj){
        boolean b=false;
        try {
            s=new Socket();
            try{
                s.connect(new InetSocketAddress("127.0.0.1",8888),2000);
            }catch(SocketTimeoutException e){
                return false;
            }
            ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(obj);
            ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
            Message ms=(Message)ois.readObject();
            System.out.println("ms = " + ms.getMsg());

            ClientConServerThread ccst=new ClientConServerThread(((IMUser)ms.getData()).getId(),s);
            System.out.println("start client thread ...... ");
            ccst.start();
            ManageClientConServer.addClientConServerThread(((IMUser)ms.getData()).getId(), ccst);
            b=true;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return b;
    }


}
